package email.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import email.controller.RefreshMailTableListener;
import email.model.Email;
import email.model.MailServer;

/**
 * 
 * @author David Froelich
 *
 */
public class Login {
	public Login(){
		createLogin();
	}
	
	/**
	 * Create the login frame
	 */
	public void createLogin(){
		final JDialog dialogLogin = new JDialog();
		dialogLogin.setTitle("Login");
		dialogLogin.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialogLogin.getContentPane().setLayout(new BorderLayout());
		
	
		JLabel labPassword = new JLabel("Password");
		final JPasswordField txtPassword = new JPasswordField(20);
		JButton btnLogin = new JButton("Login");
		JButton btnSettings = new JButton("Settings");
		txtPassword.setText("");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MailServer.getInstance().setPassword(txtPassword.getPassword());
				ArrayList<Email> mails=RefreshMailTableListener.Mailsabrufen();
				if(mails!=null){
					MainView mainView = new MainView(mails);
					dialogLogin.dispose();
				}
			}
		});
		btnSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SettingsDialog settings = new SettingsDialog();
			}
		});
		
		JPanel loginpanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		loginpanel.add(labPassword);
		loginpanel.add(txtPassword);
		
		JPanel loginpanelbtn = new JPanel(new FlowLayout(FlowLayout.CENTER));
		loginpanelbtn.add(btnLogin);
		loginpanelbtn.add(btnSettings);
		
		dialogLogin.add(loginpanel, BorderLayout.CENTER);
		
		dialogLogin.add(loginpanelbtn, BorderLayout.SOUTH);
		dialogLogin.pack();
		dialogLogin.setVisible(true);
	}
}
